﻿Public Class TimeHelpers
  Public Shared Function ConvertSecondstoCountdownTime(ByVal Seconds As Integer) As String

    Dim Result As String
    Dim Minutes As Integer
    Dim Hours As Integer
    Dim RemainingSeconds As Integer
    Dim RemainingMinutes As Integer

    If Seconds < 60 Then
      Result = Seconds.ToString

    Else

      Minutes = Seconds \ 60

      If Minutes > 59 Then
        Hours = Minutes \ 60
        RemainingMinutes = Minutes - (Hours * 60)
      End If

      RemainingSeconds = Seconds - (Minutes * 60)

      If Hours < 1 Then
        If RemainingSeconds < 10 Then
          Result = String.Format("{0:G}:0{1:G}", Minutes, RemainingSeconds)
        Else
          Result = String.Format("{0:G}:{1:G}", Minutes, RemainingSeconds)
        End If
      Else
        If RemainingMinutes < 10 Then
          If RemainingSeconds < 10 Then
            Result = String.Format("{0:G}:0{1:G}:0{2:G}", Hours, RemainingMinutes, RemainingSeconds)
          Else
            Result = String.Format("{0:G}:{1:G}:{2:G}", Hours, RemainingMinutes, RemainingSeconds)
          End If
        Else
          If RemainingSeconds < 10 Then
            Result = String.Format("{0:G}:{1:G}:0{2:G}", Hours, RemainingMinutes, RemainingSeconds)
          Else
            Result = String.Format("{0:G}:{1:G}:{2:G}", Hours, RemainingMinutes, RemainingSeconds)
          End If

        End If

      End If

    End If

    Return Result

  End Function

End Class
